package com.sctu.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.sctu.EMSApp.R;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.ScenicTourBean;

import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhu on 2015/11/3.
 */
public class ScenictourIntroduceFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private ViewPager vp;
    private ImageView itemImageView;
    private TextView textView_current, textView_all, textContent, textTitle;
    private List<ScenicTourBean.Results> scenicTourBeanList;
    private RequestQueue request;
    private View view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.fragment_scenictour_introduce, container, false);
            initViews();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    private void initViews() {
        vp = (ViewPager) view.findViewById(R.id.scenic_tour_viewPager);
        vp.setOnPageChangeListener(ScenictourIntroduceFragment.this);
        initData();
    }

    public void initData() {
        request = Volley.newRequestQueue(getActivity());
            GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_SCENIC_TOUR,
                    listener, errorlistener);
    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            List<ScenicTourBean.Results> scenicTourBeanList = JSON.parseObject(jsonstring, ScenicTourBean.class).getResults();
            List<View> viewList = new ArrayList<View>();
            for (int i = 0; i < scenicTourBeanList.size(); i++) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_scenic_tour, null);
                textView_current = (TextView) view.findViewById(R.id.scenic_tour_current);
                textView_all = (TextView) view.findViewById(R.id.scenic_tour_page_all);
                textContent = (TextView) view.findViewById(R.id.scenic_tour_content);
                textTitle = (TextView) view.findViewById(R.id.scenic_tour_title);
                itemImageView = (ImageView) view.findViewById(R.id.item_scenic_tour_image);
                //设置图片
                GetVolleyHttp.getInstance(getActivity()).setImageLoader(itemImageView,
                        R.drawable.scenerry, R.drawable.error_image, UrlConfig.URL + scenicTourBeanList.get(i).getPath());
                textTitle.setText(scenicTourBeanList.get(i).getTitle());
                textContent.setText(scenicTourBeanList.get(i).getContent());
                textView_current.setText(i + 1 + "");
                textView_all.setText("/  " + scenicTourBeanList.size() + "");
                viewList.add(view);
            }
            //建立适配器，并绑定数据源绑定适配器到listview
            vp.setAdapter(new ViewPagerAdapter(viewList));
        }

    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {

            if(request.getCache().get(UrlConfig.URL_SCENIC_TOUR) != null){   //使用缓存
                //response exists
                getchache();
            }else {
                Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                        .show();
            }



        }
    };

    private void getchache(){
        String catchData = new String(request.getCache().get(UrlConfig.URL_SCENIC_TOUR).data);
        List<ScenicTourBean.Results> scenicTourBeanList = JSON.parseObject(catchData, ScenicTourBean.class).getResults();
        Log.i("jsonData",catchData);
        List<View> viewList = new ArrayList<View>();
        for (int i = 0; i < scenicTourBeanList.size(); i++) {
            view = LayoutInflater.from(getActivity()).inflate(R.layout.item_scenic_tour, null);
            textView_current = (TextView) view.findViewById(R.id.scenic_tour_current);
            textView_all = (TextView) view.findViewById(R.id.scenic_tour_page_all);
            textContent = (TextView) view.findViewById(R.id.scenic_tour_content);
            textTitle = (TextView) view.findViewById(R.id.scenic_tour_title);
            itemImageView = (ImageView) view.findViewById(R.id.item_scenic_tour_image);
            //设置图片
            GetVolleyHttp.getInstance(getActivity()).setImageLoader(itemImageView,
                    R.drawable.scenerry, R.drawable.error_image, UrlConfig.URL + scenicTourBeanList.get(i).getPath());
            textTitle.setText(scenicTourBeanList.get(i).getTitle());
            textContent.setText(scenicTourBeanList.get(i).getContent());
            textView_current.setText(i + 1 + "");
            textView_all.setText("/ " + scenicTourBeanList.size() + "");
            viewList.add(view);
        }
        //建立适配器，并绑定数据源绑定适配器到listview
        vp.setAdapter(new ViewPagerAdapter(viewList));
    }



    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {

    }

}
